package com.gdit.zax.utils;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TextUtil {
	public static int String2int (String str,int defvar) {
		try {
			return Integer.parseInt(str);
		} catch (Exception e) {
		}
		return defvar;
	}
	
	public static boolean isEmpty (String str) {
		if (str == null || str.equals("")) {
			return true;
		}
		return false;
	}
	
	
	/**
	 * http://ddddd/1.jsp?dhfg=dhbf
	 * 获取1.jsp的算法
	 * @param url
	 * @return
	 */
	public static String getUrlLastName(String url) {
		int startleft = url.lastIndexOf('/');
		if (startleft < 0) {
			startleft = 0;
		} else {
			startleft++;
		}
		
		int endright = url.indexOf('?');
		if (endright < 0) {
			endright = url.length();
		}
		
		return url.substring(startleft, endright);
	}
	
	/**
	 * http://ddddd/1.jsp?dhfg=dhbf
	 * 获取http://ddddd/1.jsp的算法
	 * @param url
	 * @return
	 */
	public static String getUrlPath (String url) {
		int endright = url.indexOf('?');
		if (endright < 0) {
			endright = url.length();
		}
		return url.substring(0, endright);
	}
	
	
	/**
	 * 文本换序
	 * 1406799117967>>7496199177061>>1406799117967
	 * @param str
	 * @return
	 */
	public static String reverse(String str) {
		int strlength = str.length();
		char[] newstr = str.toCharArray();
		for (int i = 0; i < strlength / 2; i++) {
			//索引是偶数时,和后面对称位置的字符对换，否则不对换
			if (i % 2 == 0) {
				char lchar = newstr[i];
				newstr[i] = newstr[strlength-i-1];
				newstr[strlength-i-1] = lchar;
			}
		}
		return new String(newstr);
	}
	
	
	/**
	 * 判断是否全部有字母和数字组成
	 * 
	 * @param name
	 * @return
	 */
	public static boolean isLimit(String name) {
		// ^.[a-zA-Z]\w{m,n}$ 由m-n位的字母数字或下划线组成
		Pattern p = Pattern.compile("[a-zA-Z0-9_]*");
		Matcher m = p.matcher(name);
		return m.matches();
	}

	/**
	 * 判断是否是电话号码
	 * 
	 * @param mobiles
	 * @return
	 */
	public static boolean isMobileNumber(String mobiles) {
		Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0-9]))\\d{8}$");
		Matcher m = p.matcher(mobiles);
		return m.matches();
	}

	public static String getdistance(String dis) {
		int disint = -1;
		try {
			disint = Integer.parseInt(dis);
		} catch (Exception e) {
		}
		return getdistance(disint);

	}

	/**
	 * 从米获取距离
	 * 
	 * @return
	 */
	public static String getdistance(int dis) {
		if (dis < 0)
			return "未知";

		String resu;
		// if (dis>=1000000) {
		// double Kdist = dis/1000;
		// // resu = (int)Kdist+ "k千米";
		// resu = (int)Kdist + "千米";
		// } else
		if (dis >= 1000) {
			double Kdist = dis / 1000.0;
			resu = String.format("%.2f", Kdist) + "千米";// 保留两位小数
		} else {
			resu = dis + "米";// 原始距离
		}

		return resu;
	}

	/**
	 * 判断email格式是否正确
	 */
	public static boolean isEmail(String email) {
		String str = "^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
		Pattern p = Pattern.compile(str);
		Matcher m = p.matcher(email);
		return m.matches();
	}
	
}
